const app = getApp();

Page({
    data: {
      ganzhi: {},

      id: 0,
      gong_detail: {},
      gua_index: 0,
      gua: {},
      gua_seed: 0,
      open_gc: false,

      news_list: [],  // 阁新闻列表
      news_open: false,

      goods_list: [],
      goods_open: false,

      zt_list: [],
      zt_open: false,

      preload_list: []
    },
    onLoad(options) {
      this.setData({ ganzhi: app.get_ganzhi() });

      this.data.id = options.id;
      this.getGongDetail(() => {
        this.setData({ gua: this.data.gong_detail.gualist[this.data.gua_index] });
        this.data.gua_index++;

        this.roll();
      });

      this.geNewsList();
      this.goodsList();
      this.ztList();
    },
    onUnload() {
      clearInterval(this.data.gua_seed);
    },
    roll() {
      this.data.gua_seed = setInterval(() => {
        this.setData({ gua: this.data.gong_detail.gualist[this.data.gua_index] });
        if (this.data.gua_index !== this.data.gong_detail.gualist.length - 1) {
          this.data.gua_index++;
        } else {
          this.data.gua_index = 0;
        }
      }, 2000);
    },
    // 获取宫详情
    getGongDetail(callback) {
      app.ajax('index/getGongDetail', { id: this.data.id }, res => {
        app.format_img(res);
        app.format_img_arr(res.gualist);

        // 预加载
        let img_arr = app.get_column(res.gualist, 'pic');
        this.setData({ preload_list: this.data.preload_list.concat(img_arr) });

        this.setData({ gong_detail: res });
        wx.setNavigationBarTitle({ title: res.title });

        if (callback) {
          callback();
        }
      });
    },
    // 获取阁新闻
    geNewsList() {
      let post = {
        ge_id: this.data.id,
        page: 1,
        perpage: 100
      };

      app.ajax('index/geNewsList', post, res => {
        app.format_img_arr(res);
        this.setData({ news_list: res });
      });
    },
    // 切换新闻展开/收起
    toggle_news() {
      this.setData({ news_open: !this.data.news_open });
    },
    // 获取商品列表
    goodsList() {
      let post = {
        cate_id: this.data.id,
        page: 1,
        perpage: 100
      };

      app.ajax('shop/goodsList', post, res => {
        app.format_img_arr(res, 'cover');
        this.setData({ goods_list: res });
      });
    },
    // 切换新闻展开/收起
    toggle_goods() {
      this.setData({ goods_open: !this.data.goods_open });
    },
    // 主题列表
    ztList() {
      let post = {
        gong_id: this.data.id,
        page: 1,
        perpage: 100
      };

      app.ajax('index/ztList', post, res => {
        app.format_img_arr(res);
        this.setData({ zt_list: res });
      });
    },
    // 切换主题展开/收起
    toggle_zt() {
      this.setData({ zt_open: !this.data.zt_open });
    },
    // 去卦详情
    to_gua_detail(e) {
      wx.navigateTo({ url: '/pages/gua-detail/gua-detail?id=' + e.currentTarget.dataset.id });
    },
    // 去主题详情
    to_zhuti_detail(e) {
      wx.navigateTo({ url: '/pages/zhuti-detail/zhuti-detail?id=' + e.currentTarget.dataset.id });
    },
    // 去阁详情
    to_ge_detail(e) {
      wx.navigateTo({ url: '/pages/ge-detail/ge-detail?id=' + e.currentTarget.dataset.id });
    },
    // 去器详情
    to_qi_detail(e) {
      wx.navigateTo({ url: '/pages/qi-detail/qi-detail?id=' + e.currentTarget.dataset.id });
    },
    toggle_gc() {
      this.setData({ open_gc: !this.data.open_gc });
    },
    roll_ts() {
      clearInterval(this.data.gua_seed);
    },
    roll_te() {
      this.roll();
    }
  }
);